package com.fjeld.middleware.activemq;

import com.fjeld.middleware.jaxb.ObjectFactory;
import com.fjeld.middleware.jaxb.V1Message;
import com.fjeld.middleware.jaxb.V1MessageAuditTrail;
import com.fjeld.middleware.util.collection.ObservableArrayList;
import com.fjeld.swingx.table.ObserverTableModel;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: fjeld
 * Date: 29/12/2011
 * Time: 11:17
 *
 * Returns correct cell values from the V1Messages in the observable list.
 */
public class MessagesTableModel  extends ObserverTableModel
{
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS"),
            latencyDateFormat = new SimpleDateFormat("ss.SSS");
    private Map<String, V1MessageAuditTrail> messageAuditTrailMap = new HashMap<String, V1MessageAuditTrail>(5000);
    private List<String> cidKeyList = new ArrayList<String>(5000);

    public MessagesTableModel()
    {
        super("CorrespondenceID", "Type", "Status", "Sending Time", "Received Time", "WireLatency", "ProcLatency", "Last Update");
    }

    @Override
    public int getRowCount()
    {
        return cidKeyList.size();
    }

    @Override
    public Object getValueAt(int row, int col)
    {
        Object value = cidKeyList.get(row);

        if (null != value)
        {
            String CID = (String) value;

            V1MessageAuditTrail v1MessageAuditTrail = messageAuditTrailMap.get(CID);

            switch (col)
            {
                case 0: return v1MessageAuditTrail.getCorrespondenceID();
                case 1: return v1MessageAuditTrail.getCorrespondenceType();
                case 2: return v1MessageAuditTrail.getCorrespondenceStatus();
                case 3: return dateFormat.format(new Date(v1MessageAuditTrail.getSendingTime()));
                case 4: return dateFormat.format(new Date(v1MessageAuditTrail.getReceiveTime()));
                case 5: return latencyDateFormat.format(new Date(v1MessageAuditTrail.getLatency()));
                case 6: return latencyDateFormat.format(new Date(v1MessageAuditTrail.getProcessingLatency()));
                case 7: return dateFormat.format(new Date(v1MessageAuditTrail.getLastUpdateTime()));
                default: return "";
            }
        }

        return null;
    }

    @Override
    public void update(Observable observable, Object o)
    {
        // The list being observed will only have messages added to it. When added, check if it's in the map.
        V1Message v1Message = (V1Message) o;
        String CID = v1Message.getCorrespondenceID();

        if (messageAuditTrailMap.containsKey(CID))
        {
            messageAuditTrailMap.get(CID).update(v1Message);
        } else
        {
            cidKeyList.add(CID);
            V1MessageAuditTrail v1MessageAuditTrail = ObjectFactory.OBJECT_FACTORY.createV1MessageAuditTrail(v1Message);
            messageAuditTrailMap.put(CID, v1MessageAuditTrail);
        }

        fireTableDataChanged();
    }
}
